<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="项目名称" size="small">
            <el-input v-model="searchForm.projectName"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()"
                 v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 ">添加
      </el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="440px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
          <el-form-item prop="projectNo" label="工程号" size="small">
            <el-input type="text" v-model="teamRow.projectNo"></el-input>
          </el-form-item>
          <el-form-item prop="projectName" label="项目名称" size="small">
            <el-input type="text" v-model="teamRow.projectName"></el-input>
          </el-form-item>
          <el-form-item prop="time" label="日期" size="small">
            <el-date-picker
              v-model="teamRow.time"
              type="date"
              value-format="timestamp"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="profession" label="专业" size="small">
            <el-input type="text" v-model="teamRow.profession"></el-input>
          </el-form-item>
          <el-form-item label="一审用时" size="small">
            <el-input type="number" v-model.number="teamRow.firstTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="二审用时" size="small">
            <el-input type="number" v-model.number="teamRow.twoTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="三审用时" size="small">
            <el-input type="number" v-model.number="teamRow.threeTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="四审用时" size="small">
            <el-input type="number" v-model.number="teamRow.fourTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="五审用时" size="small">
            <el-input type="number" v-model.number="teamRow.fiveTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="六审用时" size="small">
            <el-input type="number" v-model.number="teamRow.sixTime">
              <span slot="suffix">分钟</span>
            </el-input>
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="100"
        v-if="roleType===0 || roleType===1 || roleType===2 || roleType===10 || roleType===13 "
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.drawingId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getList,
    addDrawingEfficiency,
    selectDrawingEfficiency,
    deleteDrawingEfficiency,
    updateDrawingEfficiency
  } from '@/api/drawingefficiency'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'
  import { isvalidPhone } from '@/utils/validate'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const sum = function(row, column) {
    return row.firstTime + row.twoTime + row.threeTime + row.fourTime + row.fiveTime + row.sixTime
  }

  const average = function(row, column) {
    return Math.round((row.firstTime + row.twoTime + row.threeTime + row.fourTime + row.fiveTime + row.sixTime) / 6 * 100) / 100
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'projectNo', label: '工程号', width: '150' },
          { prop: 'projectName', label: '项目名称', width: '300' },
          { prop: 'time', label: '日期', width: '100', component: time },
          { prop: 'profession', label: '专业', width: '100' },
          { prop: 'firstTime', label: '一审用时(分钟)', width: '120' },
          { prop: 'twoTime', label: '二审用时(分钟)', width: '120' },
          { prop: 'threeTime', label: '三审用时(分钟)', width: '120' },
          { prop: 'fourTime', label: '四审用时(分钟)', width: '120' },
          { prop: 'fiveTime', label: '五审用时(分钟)', width: '120' },
          { prop: 'sixTime', label: '六审用时(分钟)', width: '120' },
          { label: '总时长(分钟)', width: '120', formatter: sum },
          { label: '平均时长(分钟)', width: '120', formatter: average },
          { prop: 'remark', label: '备注说明', width: '200' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        costTypes: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增审图效率',
        marginTitle: '',
        marginType: 1,
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        margin: {},
        marginClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        marginVisible: false,
        pca: pca,
        rules: {
          time: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          projectNo: [
            { required: true, message: '请输入工程号', trigger: 'change' }
          ],
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'change' }
          ]
        },
        visible: false
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          console.log(this.tableData)
          this.listLoading = false
        })
      },
      provinceChange() {
        this.searchForm.cityId = ''
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增审图效率'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(drawingId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改审图效率'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        const params = {
          drawingId: drawingId
        }
        selectDrawingEfficiency(params).then(response => {
          this.teamRow = response.responseBody
        })
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          projectNo: this.teamRow.projectNo,
          projectName: this.teamRow.projectName,
          time: this.teamRow.time,
          profession: this.teamRow.profession || '',
          firstTime: this.teamRow.firstTime || 0,
          twoTime: this.teamRow.twoTime || 0,
          threeTime: this.teamRow.threeTime || 0,
          fourTime: this.teamRow.fourTime || 0,
          fiveTime: this.teamRow.fiveTime || 0,
          sixTime: this.teamRow.sixTime || 0,
          remark: this.teamRow.remark || ''
        }
        if (!this.dialogType) {
          params.drawingId = this.teamRow.drawingId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addDrawingEfficiency(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateDrawingEfficiency(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          drawingId: scope.row.drawingId,
          isDel: 1
        }
        deleteDrawingEfficiency(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
